<?php
defined('_XR_EXEC') or die();
class eobject {
	var $_log = null;
	function __construct() {
		
	}
	function eobject() {
		$args = func_get_args();
		if (method_exists($this, '__destruct')) {
			register_shutdown_function(array (
				& $this,
				'__destruct'
			));
		}
		call_user_func_array(array (& $this,'__construct'), $args);
	}
	function set($property, $value = null) {
		return $this-> $property = $value;
	}
	function get($property, $default = null) {
		if (isset ($this-> $property)) {
			return $this-> $property;
		} else {
			return $default;
		}
	}
	function save($file, &$data) {
		//debug::show($file);
		$dir = dirname($file);
		if(empty($data)){
			return false;
		}
		if (!is_dir($dir)) {
			if (!$this->mkdirs($dir) || !is_writable($dir)) {
				trigger_error('Directory dose not writable.', 'E_USER_WARNING');
				return false;
			}
		}
		$handle = fopen($file, 'w');
		if (flock($handle, LOCK_EX)) { //LOCK_EX 独占锁定
			fwrite($handle, $data);//sleep(6);
			flock($handle, LOCK_UN);
			fclose($handle);
			return true;
		} else {
			trigger_error('File:' . basename($file) . ' is busing now....', 'E_USER_WARNING');
			return false;
		}
	}
	function mkdirs($dir, $mode = 0777) {
		if (is_dir(dirname($dir)) && @mkdir($dir, $mode)){
			return true;
		}else {
			if ($this->mkdirs(dirname($dir), $mode) === true)
				return $this->mkdirs($dir);
		}
	}	
	function setFrom(& $data) {
		if (is_array($data) && $data) {
			$valid = array_keys(get_class_vars(get_class($this)));
			foreach ($data as $var => $val) {
				if (in_array($var, $valid))
					$this-> $var = $val;
			}
		}
	}
	function getPublicProperties() {
		static $cache = null;
		if (is_null($cache)) {
			$cache = array ();
			foreach (get_class_vars(get_class($this)) as $key => $val) {
				if (substr($key, 0, 1) != '_') {
					$cache[] = $key;
				}
			}
		}
		return $cache;
	}
	function toString() {
		return get_class($this);
	}
}
?>